package edu.ccut.saturn.basecomponent.databasecomponent;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import edu.ccut.saturn.basecomponent.database.util.SaturnMap;
import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameter;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnComponentException;

public abstract class AbstractExecuteBySaturnSQL extends DefaultComponent {
	@SuppressWarnings("unchecked")
	protected Map<String, Object> getCustomMap(List<String> expList,
			IParameterContext parameterContext) throws SaturnComponentException {
		Map<String, Object> customMap = new SaturnMap(parameterContext);
		for (String item : expList) {
			item = item.trim();
			//item = item.substring(1, item.length() - 1);
			String[] items = item.split("=");
			if (items.length < 2) {
				throw new SaturnComponentException("Expression is not valid");
			}
			String key = items[0].trim();
			String value = items[1].trim();
			value = parseCondtion(value, parameterContext);
			customMap.put(key, value);
		}
		return customMap;
	}

	protected List<String> getParameterStr(IParameterContext parameterContext) {
		List<String> expList = new ArrayList<String>();
		List<IParameter> paramList = parameterContext.getParameters();

		for (int i = 3; i < paramList.size(); i++) {

			IParameter parameter = (IParameter) paramList.get(i);
			String expression = (String) parameter.getValue();
			expList.add(expression);

		}
		return expList;
	}

	/**
	 * 解析大括号中的内容，得到新的String user.name -> aa n == ${user.name} + ${user.age} -> n ==
	 * 'aa' + '10'
	 * 
	 * @param condition
	 *            形如 'user.age between ${startAge} and ${endAge}' 'user.name
	 *            =${user.name}'
	 * @param context
	 * @return
	 */
	protected String parseCondtion(String condition,
			IParameterContext parameterContext) {
		StringBuffer newCondition = new StringBuffer().append(""); //$NON-NLS-1$
		char[] charArray = condition.toCharArray();
		StringBuffer oldexp = new StringBuffer();// ${}表达式
		boolean isGetExp = false; // 是否在拼${}表达式
		boolean is$ = false;
		for (char currItem : charArray) {
			if (is$) {
				if (currItem == '{') {
					// 开始拼表达式
					isGetExp = true;
					oldexp.append("${"); //$NON-NLS-1$
				} else {
					// 只是普通$符号
					newCondition.append("$").append(currItem); //$NON-NLS-1$
				}
				is$ = false;
			} else {
				if (isGetExp) {
					if (currItem != '}') {
						oldexp.append(currItem);
					} else {
						oldexp.append(currItem);
						isGetExp = false;
						// 解析
						Object value = parameterContext
								.getLogicContextValue(oldexp.toString());

						if (value != null && !"".equals((value+"").trim())) {
							newCondition.append(value);
						} else {
							debug(oldexp
									+ Messages
											.getString("GetDynamicCondition.1"), parameterContext); //$NON-NLS-1$ //$NON-NLS-2$
							return "";
						}
						oldexp.delete(0, oldexp.length());
					}
				} else {
					if (currItem != '$') {
						newCondition.append(currItem);
					} else {
						// 判断是否开始拼表达式
						is$ = true;
					}
				}
			}
		}
		return newCondition.toString();
	}
}
